﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace BOMS.WebUI.Web.DataAnalyse
{
    public partial class singleHistory : System.Web.UI.Page
    {
        private BLL.S_BatSnapshot bll = new BLL.S_BatSnapshot();
        protected void Page_Load(object sender, EventArgs e)
        {
            drpEquip.SelectedIndexChanged += new EventHandler(drp_SelectedIndexChanged);
            drpBatgrp.SelectedIndexChanged += new EventHandler(drp_SelectedIndexChanged);
            btnQuery.Click += new EventHandler(btnQuery_Click);
            if (!IsPostBack)
            {
                BindDropdownlist(drpEquip);
            }
           
        }
        private int BindData()
        {
            StringBuilder strWhere = new StringBuilder(128);

            strWhere.AppendFormat("Generation={0} and EquipId='{1}' and BatteryGroup='{2}' and BatteryID='{3}'"
          , Common.GENRATION_NUM, drpEquip.SelectedValue, drpBatgrp.SelectedValue, drpBattery.SelectedValue);

            string str1 = txtDtmBegin.Text.Trim();
            string str2 = txtDtmEnd.Text.Trim();
            DateTime t1, t2;
            if (str1 != "" && DateTime.TryParse(str1, out t1))
            {
                strWhere.AppendFormat(" and LogTime>='{0}'",str1);
            }

            if (str2 != "" && DateTime.TryParse(str2, out t2))
            {
                strWhere.AppendFormat(" and LogTime<='{0}'", str2);
            }

            BLL.S_BatSubLog logbll = new BLL.S_BatSubLog();
      
            //传入sql语句，分页
            int pageSize = Pager1.PageSize;
            int curPage = Pager1.CurrentPageIndex;
            int recordCount;

            List<Model.S_BatSubLog> logList = logbll.GetModelListFromPage(strWhere.ToString(),"LogTime ASC",pageSize,curPage,out recordCount);
            this.Pager1.RecordCount = recordCount;

            Repeater1.DataSource = logList;
            Repeater1.DataBind();

           
            this.Label_begin.Text = this.txtDtmBegin.Text==""?"上线":this.txtDtmBegin.Text;
            this.Label_end.Text = this.txtDtmEnd.Text == "" ? "今天" : this.txtDtmEnd.Text;
            this.Label_count.Text = recordCount.ToString();
            this.Label_wince.Text = this.drpEquip.SelectedItem.Text;
            this.Label_group.Text = this.drpBatgrp.SelectedItem.Text;
            Label_battery.Text = this.drpBattery.SelectedItem.Text;

            Object[] objs = {drpEquip.Text,
                drpBatgrp.SelectedItem.Text,
                drpBattery.SelectedItem.Text,
                txtDtmBegin.Text,
                txtDtmEnd.Text};


            Button2.PostBackUrl = string.Format("singleHistoryPic.aspx?generation="+Common.GENRATION_NUM+"&equipid={0}&batgrp={1}&batid={2}&dtmbegin={3}&dtmend={4}",
                objs
                );
            return recordCount;
            
        }
        protected void Pager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }
        void btnQuery_Click(object sender, EventArgs e)
        {
           int n= BindData();
           if (n > 0)
           {
               Button2.Enabled = true;
               Button3.Enabled = true;
           }
        }
        void BindDropdownlist(object aim)
        {

            DropDownList drp = (DropDownList)aim;

            List<string> li = null;
            Dictionary<string, string> dict = null;
            if (drp == drpEquip)
            {
                // li = bll.GetEquipList();
                dict = bll.GetEquipList();

                foreach (KeyValuePair<string, string> kvp in dict)
                {
                    ListItem Item = new ListItem(kvp.Value, kvp.Key);
                    if (!drp.Items.Contains(Item))
                    {
                        drp.Items.Add(Item);
                    }
                }

            }
            else if (drp == drpBatgrp)
            {
                li = bll.GetBatgrpList(drpEquip.SelectedValue);
                //li.Add("0");//通信中断时，查不到电池组信息

                drp.Items.Clear();
                ListItem defaultItem = new ListItem("请选择电池组", "");
                drp.Items.Add(defaultItem);

                for (int i = 0; i < li.Count; i++)
                {
                    ListItem it = new ListItem(li[i], li[i]);
                    if (!drp.Items.Contains(it))
                    {
                        drp.Items.Add(it);

                    }
                }
            }
            else if (drp == drpBattery)
            {
                li = bll.GetBatteryList(drpEquip.SelectedValue, drpBatgrp.SelectedValue);

                drp.Items.Clear();
                ListItem defaultItem = new ListItem("请选择电池", "");
                drp.Items.Add(defaultItem);

                for (int i = 0; i < li.Count; i++)
                {
                    ListItem it = new ListItem(li[i], li[i]);
                    if (!drp.Items.Contains(it))
                    {
                        drp.Items.Add(it);
                    }
                }
            }
          
        }
        void drp_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList drp = (DropDownList)sender;
            if (drp == drpEquip)
            {
                BindDropdownlist(drpBatgrp);
            }
            else if (drp == drpBatgrp)
            {
                BindDropdownlist(drpBattery);
            }
        }

        //protected void drpEquip_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    DropDownList drp = (DropDownList)sender;
        //    if (drp == drpEquip)
        //    {
        //        BindDropdownlist(drpBatgrp);
        //    }
        //    else if (drp == drpBatgrp)
        //    {
        //        BindDropdownlist(drpBattery);
        //    }
        //}
        private List<Model.S_BatSubLog> getAllDataForExcel()
        {
            StringBuilder strWhere = new StringBuilder(128);

            strWhere.AppendFormat("Generation={0} and EquipId='{1}' and BatteryGroup='{2}' and BatteryID='{3}'"
          , Common.GENRATION_NUM, drpEquip.SelectedValue, drpBatgrp.SelectedValue, drpBattery.SelectedValue);

            string str1 = txtDtmBegin.Text.Trim();
            string str2 = txtDtmEnd.Text.Trim();
            DateTime t1, t2;
            if (str1 != "" && DateTime.TryParse(str1, out t1))
            {
                strWhere.AppendFormat(" and LogTime>='{0}'", str1);
            }

            if (str2 != "" && DateTime.TryParse(str2, out t2))
            {
                strWhere.AppendFormat(" and LogTime<='{0}'", str2);
            }

            BLL.S_BatSubLog logbll = new BLL.S_BatSubLog();

            //传入sql语句，分页
            int pageSize = 100000;
            int curPage = 1;
            int recordCount;

            List<Model.S_BatSubLog> logList = logbll.GetModelListFromPage(strWhere.ToString(), "LogTime ASC", pageSize, curPage, out recordCount);
            this.Pager1.RecordCount = recordCount;

            return logList;
            
        }
        private void gridviewAddColumn(GridView gv)
        {
            BoundField field;

            field = new BoundField();
            field.HeaderText = "记录号";
            field.DataField = "LogID";
            gv.Columns.Add(field);

            field = new BoundField();
            field.HeaderText = "主机地址";
            field.DataField = "EquipID";
            gv.Columns.Add(field);

            field = new BoundField();
            field.HeaderText = "电池组";
            field.DataField = "BatteryGroup";
            gv.Columns.Add(field);

            field = new BoundField();
            field.HeaderText = "电池";
            field.DataField = "BatteryID";
            gv.Columns.Add(field);

            field = new BoundField();
            field.HeaderText = "电压(V)";
            field.DataField = "Vval";
            gv.Columns.Add(field);

            field = new BoundField();
            field.HeaderText = "内阻(mΩ)";
            field.DataField = "Rval";
            gv.Columns.Add(field);

            field = new BoundField();
            field.HeaderText = "温度(℃)";
            field.DataField = "Tval";
            gv.Columns.Add(field);

            field = new BoundField();
            field.HeaderText = "状态";
            field.DataField = "Status";
            gv.Columns.Add(field);

            field = new BoundField();
            field.HeaderText = "时间";
            field.DataField = "LogTime";;
            gv.Columns.Add(field);
           
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            System.Web.UI.WebControls.GridView dgExport = new System.Web.UI.WebControls.GridView();
            //添加列
            gridviewAddColumn(dgExport);

            dgExport.DataSource = getAllDataForExcel();
            dgExport.AllowPaging = false;
            dgExport.AutoGenerateColumns = false;
            dgExport.DataBind();
            Response.Clear();
            Response.BufferOutput = true;
            //设定输出的字符集
            Response.Charset = "UTF-8";
            //假定导出的文件名为FileName.xls
            Response.AppendHeader("Content-Disposition", "attachment;filename=SingleHistory.xls");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            //设置导出文件的格式
            
            // --excel 2003
            //Response.ContentType = "application/vnd.ms-excel";
            // --excel 2003
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            //关闭ViewState
            EnableViewState = false;
            System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
            System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
            dgExport.RenderControl(textWriter);

            //把HTML写回浏览器
            Response.Write(stringWriter.ToString());
            //释放
            textWriter.Dispose();
            stringWriter.Dispose();
            
            Response.End();
        }

       

           
    }
}